﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EbayQueryExpertLib.Entities;
using System.Drawing;

namespace EbayQueryExpertLib.Windows.Forms
{
    public class DetailedEbayListBox : ListBox
    {
        public DetailedEbayListBox()
        {
            ItemHeight = 100;
            DrawMode = DrawMode.OwnerDrawVariable;
            Font = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Bold, GraphicsUnit.Point, ((byte)(0)));
            FormattingEnabled = true;
        }
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            SuspendLayout();
            // Make sure we're not trying to draw something that isn't there.
            if (e.Index >= Items.Count || e.Index <= -1)
                return;

            // Get the item object.
            object item = Items[e.Index];
            if (item == null)
                return;
            EbaySearchItem it = item as EbaySearchItem;
            // Draw the background color depending on 
            // if the item is selected or not.
            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            {
                // The item is selected.
                // We want a blue background color.
                e.Graphics.FillRectangle(new SolidBrush(Color.AliceBlue), e.Bounds);
            }
            else
            {
                // The item is NOT selected.
                // We want a white background color.
                e.Graphics.FillRectangle(new SolidBrush(Color.White), e.Bounds);
            }
            if (it.Icone != null)
            {
                double fullLong = Math.Max(it.Icone.Height, it.Icone.Width);
                double ratio = Math.Min(1, (ItemHeight - 10) / fullLong);
                int height = (int)(it.Icone.Height * ratio);
                int width = (int)(it.Icone.Width * ratio);
                e.Graphics.DrawImage(it.Icone, (ItemHeight - width) / 2, e.Bounds.Y + ((ItemHeight - height) / 2), width, height);
            }
            // Draw the item.
            int left = it.Icone == null ? 5 : ItemHeight;
            string text = item.ToString();
            SizeF stringSize = e.Graphics.MeasureString(text, Font);
            e.Graphics.DrawString(text, Font, new SolidBrush(ForeColor),
                new PointF(ItemHeight, e.Bounds.Y + (e.Bounds.Height - stringSize.Height) / 2));


            ResumeLayout();
        }
    }
}
